# PLUGIN_NAME = VC8
# $in BUILD or CLEANUP
# $in build directory

use strict;
use Config::INIPlus;
use Cwd;

if( scalar(@ARGV) != 2 )
{
	exit 1; 
}
my $this_script_path = Cwd::abs_path( $0 );
my @path_array = split /\//, $this_script_path;
$path_array[$#path_array] = "vc8.cfg";
my $config_file_path = join '\\', @path_array;

my $method = $ARGV[0];
my $build_dir = $ARGV[1];
my %config;

print "$ARGV[0]\n";
print "$ARGV[1]\n";

if( $method eq "BUILD" )
{
	exit Build();
}
elsif( $method eq "CLEANUP" )
{
	exit CleanUp();
}
else
{
	exit 1;
}

# $out 0 for success, 1 for failure
sub Build()
{
	my $cfg = Config::INIPlus->new( file => $config_file_path );

	$config{"devenv_path"}  = $cfg->get( "DEVENV_PATH", "MAIN" );
	$config{"configuration"}  = $cfg->get( "CONFIGURATION", "MAIN" );
	$config{"platform"}  = $cfg->get( "PLATFORM", "MAIN" );
	$config{"build_type"}  = $cfg->get( "BUILD_TYPE", "MAIN" );

	my $cmd = "\"" . $config{"devenv_path"} . "\"";
	$cmd .= " /" . $config{"build_type"};
	$cmd .= " \"" . $config{"configuration"} . "|" . $config{"platform"} . "\"";
	$cmd .= " \"" . $build_dir . "\"";

	print "$cmd\n";

	open( OUTPUT, "$cmd|" );

	my $line;
	while ( $line = <OUTPUT> )
	{
		chomp $line;
		print "OUTPUT> $line\n";

		if ( $line =~ /(.*) - (\d+) error\(s\), (\d+) warning\(s\)/ )
		{
			if ( $2 != 0 )
			{
				print "PROJECT $1 ERROR $2 WARNING $3 FAILED\n";
			}
			else
			{
				print "PROJECT $1 ERROR $2 WARNING $3 SUCCEEDED\n";
			}

		}
		elsif ( $line =~ /========== Rebuild All: (\d+) succeeded, (\d+) failed, (\d+) skipped ==========/ )
		{
			print "TOTAL $1 SUCCEEDED $2 FAILED $3 SKIPPED\n";
		}

	}


}

# $out 0 for success, 1 for failure
sub CleanUp()
{


}

